//
//  BaseCell.m
//  BaiSiBuDeJie
//
//  Created by bill on 16/8/22.
//  Copyright © 2016年 tsaievan. All rights reserved.
//

#import "BaseCell.h"
#import "BSBaseModel.h"
@interface BaseCell ()
//头像
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
//昵称
@property (weak, nonatomic) IBOutlet UILabel *nickNameLab;
//发表时间
@property (weak, nonatomic) IBOutlet UILabel *passtimeLab;
//标题
@property (weak, nonatomic) IBOutlet UILabel *titleLab;
//视频和图片
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
//标签集合
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *tagLabArray;
//按钮集合
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *btnArray;

//点赞
@property (weak, nonatomic) IBOutlet UIButton *upBtn;
//不赞
@property (weak, nonatomic) IBOutlet UIButton *downBtn;
//分享
@property (weak, nonatomic) IBOutlet UIButton *sharBtn;
//评论数
@property (weak, nonatomic) IBOutlet UIButton *commentBtn;
//播放器
@property (weak, nonatomic) IBOutlet UILabel *playCountLab;
//视频总时间
@property (weak, nonatomic) IBOutlet UILabel *videoTimeLab;
//图片的高度
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imgHeight;
//标签距图片的高度

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tagHeight;

//gif
@property (weak, nonatomic) IBOutlet UIImageView *gifImageView;

@property (weak, nonatomic) IBOutlet UIButton *videoPlayBtn;

@property (weak, nonatomic) IBOutlet UIButton *audioPlayBtn;

//赞按钮的状态
@property(nonatomic,assign)BOOL upBtnStatus;
//不赞按钮的状态
@property(nonatomic,assign)BOOL dwonBtnStatus;
//点赞数
@property(nonatomic,assign)NSInteger upCount;
//不赞赞
@property(nonatomic,assign)NSInteger downCount;

//vip
@property (weak, nonatomic) IBOutlet UIImageView *vipView;
@property (weak, nonatomic) IBOutlet UIImageView *superVip;

@end

@implementation BaseCell

+(instancetype)getCellWith:(UITableView*)tableView
{
    BaseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tabelViewCell"];
    if (cell==nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"BaseCell" owner:nil options:nil]lastObject];;
    }
    
    return cell;
}

-(void)awakeFromNib
{
    //增加按钮边框
    [self.btnArray enumerateObjectsUsingBlock:^(UIButton* btn, NSUInteger idx, BOOL * _Nonnull stop) {
        UIImage *image1 = [UIImage imageNamed:@"common_button_white"];
        image1 = [image1 stretchableImageWithLeftCapWidth:image1.size.width * 0.5 topCapHeight:image1.size.height * 0.5];
        UIImage *image2 = [UIImage imageNamed:@"common_button_white_highlighted"];
        image2 = [image2 stretchableImageWithLeftCapWidth:image2.size.width * 0.5 topCapHeight:image1.size.height * 0.5];
        
        [btn setBackgroundImage:image1 forState:UIControlStateNormal];
        [btn setBackgroundImage:image2 forState:UIControlStateHighlighted];
    }];
}


-(void)layoutSubviews
{
    [super layoutSubviews];

}

-(void)setModel:(ListModel *)model
{
    _model = model;
    self.tagHeight.constant = 20;
    
    //头像
    NSArray *urlArr = model.u.header;
    [self.iconView sd_setImageWithURL:urlArr[0]];
    self.iconView.layer.cornerRadius = self.iconView.bounds.size.width * 0.5;
    [self.iconView clipsToBounds];
    
    //是否vip
    if (model.u.is_v) {
        self.vipView.image = [UIImage imageNamed:@"Profile_AddV_authen"];
    }else{self.vipView.image = nil;}
    
    //黄金会员
    if (model.u.is_vip) {
        self.superVip.image = [UIImage imageNamed:@"nav_title_vip_icon_16x12_"];
    }else{self.superVip.image = nil;}
    
    //昵称
    self.nickNameLab.text = model.u.name;
    
    //发表时间
    self.passtimeLab.text = model.passtime;
    
    //标题
    self.titleLab.text = model.text;
    
    
    //如果是视频
    if ([model.type isEqualToString:@"video"]) {
        NSArray *imgArr = model.video.thumbnail;
        [self.imgView sd_setImageWithURL:imgArr[0]];
      
            //播放量
            self.playCountLab.text = [NSString stringWithFormat:@"%zd播放",model.video.playcount];
            
            //视频长度
            self.videoTimeLab.text = [NSString stringWithFormat:@"%02zd:%02zd",model.video.duration / 60,model.video.duration % 60];
            
           //宽度比
            
            NSInteger width = model.video.width;
            NSInteger height = model.video.height;
           self.imgHeight.constant = (kScreenW - 20) * height / width;
           
            //播放图标
        self.gifImageView.alpha = 0;
        self.videoPlayBtn.alpha = 1;
        self.audioPlayBtn.alpha = 0;
        self.videoTimeLab.alpha = 1;
        self.playCountLab.alpha = 1;
        
        //图片超出屏幕的宽则显示查看全图按钮
        //CGFloat scale = [UIScreen mainScreen].scale;
        
//        if (self.imgHeight.constant > kScreenH * 0.6) {
//            self.imgHeight.constant = kScreenH * 0.6;
//            //self.imgView .contentMode = UIViewContentModeScaleAspectFit;
//        }
    }
    
    
    //段子
    if ([self.model.type isEqualToString:@"text"])
    {
        //文本的时候把图片撤掉
        self.imgHeight.constant = 0;
        self.tagHeight.constant = 10;
        self.gifImageView.alpha = 0;
        self.videoPlayBtn.alpha = 0;
        self.audioPlayBtn.alpha = 0;
        self.videoTimeLab.alpha = 0;
        self.playCountLab.alpha = 0;
       
    }
    
    //标签
    //先移除
    for (NSInteger j = 0; j < 6; j ++) {
        UILabel *lab = self.tagLabArray[j];
        lab.text = nil;
    }
    
    for (NSInteger i = 0; i < model.tags.count; i ++) {
        UILabel *lab = self.tagLabArray[i];
        
        lab.text = model.tags[i].name;
    }
    
    //点赞数
    self.upCount = model.up.integerValue;
    [self.upBtn setTitle:[NSString stringWithFormat:@"%zd",self.upCount] forState:UIControlStateNormal];
    self.upBtn.selected = self.model.upBtnStatus;
    if (model.upBtnStatus) {
        [self.upBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }else{ [self.upBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];}
    
    
    
    //不赞
    self.downCount = model.down;
    [self.downBtn setTitle:[NSString stringWithFormat:@"%zd",self.downCount] forState:UIControlStateNormal];
    self.downBtn.selected = self.model.downBtnStatus;
    
    
    
    if (model.downBtnStatus) {
        [self.downBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }else{ [self.downBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];}
    
    //转载
    [self.sharBtn setTitle:[NSString stringWithFormat:@"%zd",model.forward] forState:UIControlStateNormal];
    
    //评论数
    [self.commentBtn setTitle:[NSString stringWithFormat:@"%@",model.comment] forState:UIControlStateNormal];
    
    //gif图片
    if ([model.type isEqualToString:@"gif"]) {
        self.gifImageView.image = [UIImage imageNamed:@"common-gif"];
        NSArray *gifArr = model.gif.images;
        [self.imgView sd_setImageWithURL:gifArr[0]];
        
        //宽高比
        NSInteger width = model.gif.width;
        NSInteger height = model.gif.height;
        self.imgHeight.constant = (kScreenW - 20) * height / width;
        
        self.gifImageView.alpha = 1;
        self.videoPlayBtn.alpha = 0;
        self.audioPlayBtn.alpha = 0;
        self.videoTimeLab.alpha = 0;
        self.playCountLab.alpha = 0;
        
        //图片超出屏幕的宽则显示查看全图按钮
//        if (model.image.height > kScreenH * 0.7) {
//            self.imgHeight.constant = kScreenH * 0.7;
//        }
    }
    
    //image图片
    if ([model.type isEqualToString:@"image"]) {
        self.gifImageView.image = nil;
       
        NSArray *imageArr = model.image.big;
        [self.imgView sd_setImageWithURL:imageArr[0]];
        
        //宽高比
        NSInteger width = model.image.width;
        NSInteger height = model.image.height;
        self.imgHeight.constant = (kScreenW - 20) * height / width;
        
        self.gifImageView.alpha = 0;
        self.videoPlayBtn.alpha = 0;
        self.audioPlayBtn.alpha = 0;
        self.videoTimeLab.alpha = 0;
        self.playCountLab.alpha = 0;
        
        //图片超出屏幕的宽则显示查看全图按钮
//        if (model.image.height > kScreenH * 0.7) {
//            self.imgHeight.constant = kScreenH * 0.7;
//        }
    }
    
    //声音
    if ([model.type isEqualToString:@"audio"]) {
        NSArray *audioArr = model.audio.thumbnail;
        [self.imgView sd_setImageWithURL:audioArr[0]];
        
        self.gifImageView.alpha = 0;
        self.videoPlayBtn.alpha = 0;
        self.audioPlayBtn.alpha = 1;
        self.videoTimeLab.alpha = 0;
        self.playCountLab.alpha = 0;
        
        NSInteger width = model.audio.width;
        NSInteger height = model.audio.height;
        self.imgHeight.constant = ((kScreenW - 20) - 20) * height / width;
    }

    
    
}


-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
}

#pragma mark - 点赞按钮的点击事件
- (IBAction)didClickUpBtn:(UIButton*)sender {
    if ( self.model.downBtnStatus || self.model.upBtnStatus) {
        NSLog(@"点击无效");
        return;
    }
    sender.selected = YES;
    self.model.upBtnStatus = !self.model.upBtnStatus;
    self.upCount++;
    [self.upBtn setTitle:[NSString stringWithFormat:@"%zd",self.upCount] forState:UIControlStateNormal];
    [sender setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    //动画
    [self btnAnimation:sender];
}
#pragma mark - 不赞按钮的点击事件
- (IBAction)didClickDownBtn:(UIButton*)sender {
    if (self.model.upBtnStatus || self.model.downBtnStatus) {
        NSLog(@"点击无效");
        return;
    }
    sender.selected = YES;
    self.model.downBtnStatus = !self.model.downBtnStatus;
    self.downCount++;
     [self.downBtn setTitle:[NSString stringWithFormat:@"%zd",self.downCount] forState:UIControlStateNormal];
    [sender setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    //动画
    [self btnAnimation:sender];
}


#pragma mark - 按钮的点击动画
-(void)btnAnimation:(UIButton*)btn
{
    
    UILabel *lab = [UILabel new];
    [self.contentView addSubview:lab];
    lab.frame = CGRectMake(0, 0, 30, 20);
    lab.text = @"+1";
    lab.textColor = [UIColor redColor];
    lab.alpha = 0.8;
    
    CGFloat X = btn.center.x + 20;
    CGFloat Y = btn.center.y - 20;
    
    
    lab.center = CGPointMake(X, Y);
    
    [UIView animateWithDuration:0.8 animations:^{
        lab.transform = CGAffineTransformScale(lab.transform, 2, 2);
        lab.alpha = 0.2;
        lab.center = CGPointMake(X + 20, Y - 20);
    } completion:^(BOOL finished) {
        [lab removeFromSuperview];
    }];
    
}





- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
